package com.llh.orgsyn;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import weaver.general.BaseBean;

/**
 * @author 刘立华
 * @version 1.0.0
 * @ClassName Syn2OAservice.java
 * @Description 按照组织结构同步接口要求，封装调用系统接口的方法
 * @createTime 2021年12月31日 11:05:00
 */
public class Syn2OACmd extends BaseBean {
    private static BaseBean logger = new BaseBean();

    /**
     * 同步分部信息
     *
     * @param subarr
     * @param listSubcompanycode
     * @return
     */
    public String synSubcompany(JSONObject subarr) {
        if (subarr != null) {
            logger.writeLog("OA内部分部同步参数=" + subarr);
            //同步分部测试
            String resp = post("/api/hrm/resful/synSubcompany", subarr.toString());
            logger.writeLog("OA内部分部同步结果=" + resp);
            boolean isJson = JSONUtil.isJsonObj(resp);
            if (isJson) {
                JSONObject respObj = JSONUtil.parseObj(resp);
                String respCode = respObj.getStr("code");
                //主数据失败直接返回
                if ("-1".equals(respCode)) {
                    return respObj.getStr("msg");
                }
                if ("1".equals(respCode)) {
                    JSONArray codearr = (JSONArray) subarr.get("codearr");
                    String resultCode;
                    for (int i = 0; i < codearr.size(); i++) {
                        String subcompanycode = (String) codearr.get(i);
                        resultCode = respObj.getJSONObject("result").getJSONObject(subcompanycode).getStr(
                                "code");
                        if (!"1".equals(resultCode)) {
                            return respObj.getJSONObject("result").getJSONObject(subcompanycode).getStr("msg");
                        }
                    }

                }
            } else {
                return "OA内部异常," + resp;
            }
        }
        return "同步成功";
    }

    private String post(String serverPath, String params) {
        String port = getPropValue("wn_xzdhrmsyn", "oaport");
        String host = "http://127.0.0.1:" + port;
        String body = HttpRequest.post(host + serverPath)
                .header("Content-Type", "application/json;charset=UTF-8")
                .body(params)
                .timeout(60 * 60 * 1000)//超时，毫秒
                .execute()
                .body();
        return body;
    }

    /**
     * 同步部门信息
     *
     * @param deptarr
     * @param listDepartmentcode
     * @return
     */
    public String synDepartment(JSONObject deptarr) {
        if (deptarr != null) {
            //同步分部测试
            logger.writeLog("OA内部部门同步参数=" + deptarr);
            String resp = post("/api/hrm/resful/synDepartment", deptarr.toString());
            logger.writeLog("OA内部部门同步结果=" + resp);
            boolean isJson = JSONUtil.isJsonObj(resp);
            if (isJson) {
                JSONObject respObj = JSONUtil.parseObj(resp);
                String respCode = respObj.getStr("code");
                //主数据失败直接返回
                if ("-1".equals(respCode)) {
                    return respObj.getStr("msg");
                }

                //主数据成功检查明细
                if ("1".equals(respCode)) {
                    JSONArray codearr = (JSONArray) deptarr.get("codearr");
                    String resultCode;
                    for (int i = 0; i < codearr.size(); i++) {
                        String departmentcode = (String) codearr.get(i);
                        resultCode = respObj.getJSONObject("result").getJSONObject(departmentcode).getStr("code");
                        if (!"1".equals(resultCode)) {
                            return JSONUtil.parseObj(resp).getJSONObject("result").getJSONObject(departmentcode).getStr("msg");
                        }
                    }
                }
            } else {
                return "OA内部异常," + resp;
            }
        }
        return "同步成功";
    }

    /**
     * 同步岗位的方法
     *
     * @param jobtitleArr
     * @param listJobtitlecode
     * @return
     */
    public String synJobtile(JSONObject jobtitleArr) {
        if (jobtitleArr != null) {

            logger.writeLog("OA内部岗位参数=" + jobtitleArr);
            //同步分部测试
            String resp = post("/api/hrm/resful/synJobtitle", jobtitleArr.toString());
            logger.writeLog("OA内部岗位同步结果=" + resp);
            boolean isJson = JSONUtil.isJsonObj(resp);
            if (isJson) {
                JSONObject respObj = JSONUtil.parseObj(resp);
                String respCode = respObj.getStr("code");
                //主数据失败直接返回
                if ("-1".equals(respCode)) {
                    return respObj.getStr("msg");
                }
                String resultCode;
                JSONArray codearr = (JSONArray) jobtitleArr.get("codearr");

                if ("1".equals(respCode)) {
                    for (int i = 0; i < codearr.size(); i++) {
                        String departmentcode = (String) codearr.get(i);
                        resultCode = respObj.getJSONObject("result").getJSONObject(departmentcode).getStr("code");
                        if (!"1".equals(resultCode)) {
                            return JSONUtil.parseObj(resp).getJSONObject("result").getJSONObject(departmentcode).getStr("msg");
                        }
                    }
                }
            } else {
                return "OA内部异常," + resp;
            }
        }
        return "同步成功";
    }

    /**
     * 同步用户信息
     *
     * @param userarr
     * @param listWorkcode
     * @return
     */
    public String synUser(JSONObject userarr) {
        if (userarr != null) {

            logger.writeLog("OA内部人员同步参数=" + userarr);
            String resp = post("/api/hrm/resful/synHrmresource", userarr.toString());
            logger.writeLog("OA内部人员同步结果=" + resp);
            boolean isJson = JSONUtil.isJsonObj(resp);
            if (isJson) {
                JSONObject respObj = JSONUtil.parseObj(resp);
                String respCode = respObj.getStr("code");
                JSONArray codearr = (JSONArray) userarr.get("codearr");
                String resultCode;
                if ("1".equals(respCode)) {

                    for (int i = 0; i < codearr.size(); i++) {
                        String workcode = (String) codearr.get(i);
                        resultCode = respObj.getJSONObject("result").getJSONObject(workcode).getStr("code");
                        if (!"1".equals(resultCode)) {
                            return JSONUtil.parseObj(resp).getJSONObject("result").getJSONObject(workcode).getStr("msg");
                        }
                    }
                }
                //主数据失败直接返回
                if ("-1".equals(respCode)) {
                    return respObj.getStr("msg");
                }

                if (!"1".equals(respCode)) {
                    return "OA内部异常," + resp;
                }
            }
        }
        return "同步成功";
    }

}
